#include "db/database.h"
#include "common/friend.h"
#include "user.h"
#include "server.h"
#include <QApplication>
#include <QCryptographicHash>

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iterator>
#include <iostream>

int main(int argc,char *argv[]) {
	QApplication app(argc,argv);
	quint16 serverPort = 1234;

	try {
		po::options_description desc("Allowed options");
		desc.add_options()
		("help", "produce help message")
		("port", po::value<quint16>(), "set server port")
		;
	
		po::variables_map vm;        
		po::store(po::parse_command_line(argc, argv, desc), vm);
		po::notify(vm);    
	
		if (vm.count("help")) {
			std::cout << desc << std::endl;
			return 0;
		}
		if (vm.count("port")) {
			serverPort = vm["port"].as<quint16>();
		} else {
			std::cout << "Port was not set. \nMore info use: server --help" << std::endl;
		}
	}
	catch(std::exception& e) {
		std::cerr << "error: " << e.what() << std::endl;
		return 2;
	}
	catch(...) {
		std::cerr << "Unespected error!" << std::endl;
		return 3;
	}
	{
		std::cout << "Running tuim server. Port: " << serverPort << std::endl;
		Database db("tuim.db");
//		db.setUser("iuri",QString(QCryptographicHash::hash(QString(123).toUtf8(), QCryptographicHash::Md5)));
//		db.setUser("charles","123");
//		db.setUser("eduardo","123");
//		db.setFriend("iuri","charles");
//		db.setFriend("iuri","eduardo");
//		db.setFriend("charles","eduardo");
	}
	Server *server = new Server(serverPort);
	app.exec();
	return 0;
}
